package com.xlf.service.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlf.common.auth.util.SecurityUtils;
import com.xlf.common.core.exception.BizException;
import com.xlf.service.blog.domain.Article;
import com.xlf.service.blog.domain.Thumbs;
import com.xlf.service.blog.mapper.ArticleMapper;
import com.xlf.service.blog.service.ThumbsService;
import com.xlf.service.blog.mapper.ThumbsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 小新
* @description 针对表【t_thumbs】的数据库操作Service实现
* @createDate 2023-07-08 18:39:10
*/
@Service
public class ThumbsServiceImpl extends ServiceImpl<ThumbsMapper, Thumbs>
    implements ThumbsService{

    @Resource
    ArticleMapper articleMapper;

    @Override
    public void thumbs(Long articleId) {

        if(!articleMapper.exists(new LambdaQueryWrapper<Article>().eq(Article::getId, articleId)))
            throw new BizException("文章不存在！");

        Object userId = SecurityUtils.getUserId();

        LambdaQueryChainWrapper<Thumbs> wrapper = lambdaQuery().eq(Thumbs::getUserId, userId)
                .eq(Thumbs::getArticleId, articleId);
        if(wrapper.exists()){
            remove(wrapper);
        }else{
            Thumbs thumbs = new Thumbs();
            thumbs.setArticleId(articleId);
            thumbs.setUserId((Long) userId);
            save(thumbs);
        }
    }
}




